package me.passtime.projt.query;

import android.app.Activity;
import me.passtime.projt.http.AbstractHttpController;
import me.passtime.projt.http.UriConstants;
import me.passtime.projt.util.PostXmlBuilder;
import me.passtime.projt.util.RequestCode;
import me.passtime.projt.util.ToastUtils;
import me.passtime.projt.util.XmlBuilder;

import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-14
 * Time: 下午1:13
 */
public class QueryController extends AbstractHttpController<String> {
    private static final int QUERY_AB = 0;//可用余额
    private static final int QUERY_CM = 1;//当前月
    private static final int QUERY_HT = 2;//历史记录
    private static final int QUERY_PR = 3;//套餐余量
    private static final int QUERY_PM = 4;//缴费记录

    private QueryAvailableBalancePage queryABPage;
    private QueryCurrentMonthPage queryCMPage;
    private QueryHistoryPage queryHTPage;
    private QueryPackageRemainPage queryPRPage;
    private QueryPaymentPage queryPMPage;

    private int currentPageIndex = 0;

    public QueryController() {
        super(String.class);
    }

    public QueryController(QueryAvailableBalancePage queryABPage) {
        super(String.class);
        this.currentPageIndex = QUERY_AB;
        this.queryABPage = queryABPage;
    }

    public QueryController(QueryCurrentMonthPage queryCMPage) {
        super(String.class);
        this.currentPageIndex = QUERY_CM;
        this.queryCMPage = queryCMPage;
    }

    public QueryController(QueryHistoryPage queryHTPage) {
        super(String.class);
        this.currentPageIndex = QUERY_HT;
        this.queryHTPage = queryHTPage;
    }

    public QueryController(QueryPackageRemainPage queryPRPage) {
        super(String.class);
        this.currentPageIndex = QUERY_PR;
        this.queryPRPage = queryPRPage;
    }

    public QueryController(QueryPaymentPage queryPMPage) {
        super(String.class);
        this.currentPageIndex = QUERY_PM;
        this.queryPMPage = queryPMPage;
    }
    


    public void queryAB(String phoneNumber) {
        String postXml = new PostXmlBuilder().setBiPCode(RequestCode.QUERY_AVAILABLE_BALANCE).setTelNo(phoneNumber).createXml();
        this.sendRequest(UriConstants.URL, postXml);
    }

    public void queryCM(String phoneNumber, String date) {
        String svcCont = "<SvcCont><TelNo>" + phoneNumber + "</TelNo><BillDate>" + date + "</BillDate></SvcCont>";
        String postXml = new PostXmlBuilder().setBiPCode(RequestCode.QUERY_MONTH_BILL).setTelNo(phoneNumber).setSvcCont(svcCont).createXml();
        this.sendRequest(UriConstants.URL, postXml);
    }
    
    public void queryHT(String phoneNumber,String date){
        queryCM(phoneNumber,date);
    }

    public void queryPR(String phoneNumber,String date){
        String postXml = new PostXmlBuilder().setBiPCode(RequestCode.QUERY_REMAIN).setTelNo(phoneNumber).createXml();
        this.sendRequest(UriConstants.URL, postXml);
    }
    public void queryPM(String phoneNumber,String date){
        String postXml=new PostXmlBuilder().setSvcCont(RequestCode.QUERY_AVAILABLE_BALANCE).setTelNo(phoneNumber).createXml();
        this.sendRequest(UriConstants.URL, postXml);
    }

    @Override
    public void onRequestFinish(String response) {
        switch (currentPageIndex) {
            case QUERY_AB:
                queryABPage.hideProgressDialog();
                if ("0000".equals(response)) {
                    queryABPage.updateAvailableBalanceUI(response);
                }
                break;
            case QUERY_CM:
                queryCMPage.hideProgressDialog();
                queryCMPage.updateCurrentMonthInfo("234.6元",new ArrayList<String>(5));
                break;
            case QUERY_HT:
                queryHTPage.hideProgressDialog();
                queryHTPage.updateListDate("WCDMA-66元A",new ArrayList<String>(5));
                break;
            case QUERY_PR:
                queryPRPage.hideProgressDialog();
                queryPRPage.updatePackageRemainInfo("WCDMA-66元A",new ArrayList<String>(5));
                break;
            case QUERY_PM:
                queryPMPage.hideProgressDialog();
                queryPMPage.updatePaymentInfo(new ArrayList<String>());
                break;
        }
    }

    @Override
    public void onNetworkAbort() {
        switch (currentPageIndex) {
            case QUERY_AB:
                queryABPage.hideProgressDialog();
                ToastUtils.showMessage(queryABPage, "错误，网络连接被中断.");
                break;
            case QUERY_CM:
                queryCMPage.hideProgressDialog();
                ToastUtils.showMessage(queryCMPage, "错误，网络连接被中断.");
                break;
            case QUERY_HT:
                queryHTPage.hideProgressDialog();
                ToastUtils.showMessage(queryHTPage, "错误，网络连接被中断.");
                break;
            case QUERY_PR:
                queryPRPage.hideProgressDialog();
                ToastUtils.showMessage(queryPRPage, "错误，网络连接被中断.");
                break;
            case QUERY_PM:
                queryPMPage.hideProgressDialog();
                ToastUtils.showMessage(queryPMPage, "错误，网络连接被中断.");
                break;
        }
    }

    @Override
    public void onNetworkError() {
        switch (currentPageIndex) {
            case QUERY_AB:
                queryABPage.hideProgressDialog();
                ToastUtils.showMessage(queryABPage, "错误，网络连接错误.");
                break;
            case QUERY_CM:
                queryCMPage.hideProgressDialog();
                ToastUtils.showMessage(queryCMPage, "错误，网络连接错误.");
                break;
            case QUERY_HT:
                queryHTPage.hideProgressDialog();
                ToastUtils.showMessage(queryHTPage, "错误，网络连接错误.");
                break;
            case QUERY_PR:
                queryPRPage.hideProgressDialog();
                ToastUtils.showMessage(queryPRPage, "错误，网络连接错误.");
                break;
            case QUERY_PM:
                queryPMPage.hideProgressDialog();
                ToastUtils.showMessage(queryPMPage, "错误，网络连接错误.");
                break;
        }
    }



    @Override
    public void onResponseError() {
        switch (currentPageIndex) {
            case QUERY_AB:
                queryABPage.hideProgressDialog();
                ToastUtils.showMessage(queryABPage, "错误，返回结果错误.");
                break;
            case QUERY_CM:
                queryCMPage.hideProgressDialog();
                ToastUtils.showMessage(queryCMPage, "错误，返回结果错误.");
                break;
            case QUERY_HT:
                queryHTPage.hideProgressDialog();
                ToastUtils.showMessage(queryHTPage, "错误，返回结果错误.");
                break;
            case QUERY_PR:
                queryPRPage.hideProgressDialog();
                ToastUtils.showMessage(queryPRPage, "错误，返回结果错误.");
                break;
            case QUERY_PM:
                queryPMPage.hideProgressDialog();
                ToastUtils.showMessage(queryPMPage, "错误，返回结果错误.");
                break;
        }
    }
}
